{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Installation and deployment of 5G System using the Open Air Interface (OAI)\n",
    "\n",
    "In this tutorial we describe how to configure and run a 5G end-to-end setup with OAI CN5G, OAI gNB and OAI nrUE.\n",
    "Minimum hardware requirements:\n",
    "\n",
    "- Laptop/Desktop/Server for OAI CN5G and OAI gNB\n",
    "    - Operating System: Ubuntu 22.04 LTS\n",
    "    - CPU: 8 cores x86_64 @ 3.5 GHz\n",
    "    - RAM: 32 GB\n",
    "- Laptop for UE\n",
    "    - Operating System: Ubuntu 22.04 LTS\n",
    "    - CPU: 8 cores x86_64 @ 3.5 GHz\n",
    "    - RAM: 8 GB\n",
    "\n",
    "\n",
    "## Objectives\n",
    "- Install and Deploy CN5G, OAIgNB and nrUE using OAI\n",
    "    - CN5G version 2.0.1\n",
    "    - OAIgNB and nrUE version 2.1.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CN5G Installation\n",
    "\n",
    "In a Linux Terminal, follow the steps below:\n",
    "\n",
    "**Step 0:** Setup your VM or Baremetal Machine, after install Useful Software\n",
    "\n",
    "**Step 1:** Install pre-requisites\n",
    "\n",
    "Open a terminal in your home directory and then execute the following commands:\n",
    "```\n",
    "sudo su\n",
    "```\n",
    "```\n",
    "apt update -y\n",
    "```\n",
    "```\n",
    "apt upgrade -y\n",
    "```\n",
    "```\n",
    "apt install -y python3-pip git curl cmake tree golang vim net-tools build-essential nfs-common openssh-server putty\n",
    "```\n",
    "```\n",
    "sudo apt install -y ca-certificates curl gnupg\n",
    "```\n",
    "```\n",
    "sudo install -m 0755 -d /etc/apt/keyrings\n",
    "```\n",
    "```\n",
    "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg\n",
    "```\n",
    "```\n",
    "sudo chmod a+r /etc/apt/keyrings/docker.gpg\n",
    "```\n",
    "```\n",
    "echo \"deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \"$(. /etc/os-release && echo \"$VERSION_CODENAME\")\" stable\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null\n",
    "```\n",
    "```\n",
    "sudo apt update\n",
    "```\n",
    "```\n",
    "sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin\n",
    "```\n",
    "```\n",
    "sudo curl -L https://github.com/docker/compose/releases/download/2.26.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose\n",
    "```\n",
    "```\n",
    "sudo usermod -a -G docker $(whoami)\n",
    "```\n",
    "\n",
    "**Step 2:** Reboot your operational system\n",
    "```\n",
    "reboot\n",
    "```\n",
    "**Step 3:** Download CN5G files\n",
    "\n",
    "Create a folder called OAI in your home directory:\n",
    "```\n",
    "mkdir ~/OAI\n",
    "```\n",
    "```\n",
    "cd OAI\n",
    "```\n",
    "```\n",
    "git clone https://gitlab.eurecom.fr/oai/cn5g/oai-cn5g-fed.git\n",
    "```\n",
    "```\n",
    "cd oai-cn5g-fed\n",
    "```\n",
    "```\n",
    "git checkout -f v2.0.1\n",
    "```\n",
    "**Step 4:** Pull the images from Docker Hub\n",
    "\n",
    "In the oai-cn5g-fed directory:\n",
    "```\n",
    "cd docker-compose\n",
    "```\n",
    "```\n",
    "docker compose -f docker-compose-basic-nrf.yaml pull\n",
    "```\n",
    "You will see:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_01.png)\n",
    "\n",
    "**Step 5:** Start OAI CN5G\n",
    "In the CN5G terminal, type:\n",
    "```\n",
    "docker compose -f docker-compose-basic-nrf.yaml up -d\n",
    "```\n",
    "You will see:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_02.png)\n",
    "\n",
    "**Step 6:** See all CN5G docker containers\n",
    "In the CN5G terminal, type:\n",
    "```\n",
    "docker ps -a\n",
    "```\n",
    "The expected visualization is similar to the figure below:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_03.png)\n",
    "\n",
    "**Step 7:** See logs of the 5GC's AMF container\n",
    "\n",
    "In the CN5G terminal, type:\n",
    "```\n",
    "docker logs -f oai-amf\n",
    "```\n",
    "The expected visualization is similar to the figure below:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_04.png)\n",
    "\n",
    "You can also use \n",
    "```\n",
    "docker logs -f ff850c663eea\n",
    "```\n",
    "where ff850c663eea is the id of the container in your machine.\n",
    "\n",
    "To leave this visualization press Ctrl + c.\n",
    "\n",
    "**Step 8:** Stop OAI 5GC\n",
    "\n",
    "In the CN5G terminal, type:\n",
    "```\n",
    "docker compose -f docker-compose-basic-nrf.yaml down\n",
    "```\n",
    "You will see:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_05.png)\n",
    "\n",
    "## OAIgNB and nrUE Installation\n",
    "\n",
    "P.S.: From this moment on, some command lines and directory adresses will have \"YOUR_USER\" in their composition, remember to change it to the user of your virtual machine.\n",
    "\n",
    "**Step 1:** Install pre-requisites\n",
    "\n",
    "Open a terminal in your home directory, then execute the following commands:\n",
    "```\n",
    "sudo apt install -y autoconf automake build-essential ccache cmake cpufrequtils doxygen ethtool g++ git inetutils-tools libboost-all-dev libncurses5 libncurses5-dev libusb-1.0-0 libusb-1.0-0-dev libusb-dev python3-dev python3-mako python3-numpy python3-requests python3-scipy python3-setuptools python3-ruamel.yaml\n",
    "```\n",
    "```\n",
    "cd OAI\n",
    "```\n",
    "```\n",
    "git clone https://github.com/EttusResearch/uhd.git\n",
    "```\n",
    "```\n",
    "cd uhd\n",
    "```\n",
    "```\n",
    "git checkout v4.6.0.0\n",
    "```\n",
    "```\n",
    "cd host\n",
    "```\n",
    "```\n",
    "mkdir build\n",
    "```\n",
    "```\n",
    "cd build\n",
    "```\n",
    "```\n",
    "cmake ../\n",
    "```\n",
    "```\n",
    "make -j $(nproc)\n",
    "```\n",
    "```\n",
    "make test\n",
    "```\n",
    "``` \n",
    "sudo make install\n",
    "```\n",
    "```\n",
    "sudo ldconfig\n",
    "```\n",
    "```\n",
    "sudo uhd_images_downloader\n",
    "```\n",
    "\n",
    "**Step 2:** Download OAIgNB and nrUE files\n",
    "\n",
    "Open a terminal in OAI directory, then execute the following command lines:\n",
    "```\n",
    "git clone https://gitlab.eurecom.fr/oai/openairinterface5g.git\n",
    "```\n",
    "```\n",
    "cd openairinterface5g\n",
    "```\n",
    "```\n",
    "git checkout v2.1.0\n",
    "```\n",
    "\n",
    "**Step 3:** Build the configuration files\n",
    "\n",
    "Open a terminal in the openairinterface5g repository:\n",
    "```\n",
    "cd cmake_targets\n",
    "```\n",
    "```\n",
    "./build_oai -I\n",
    "```\n",
    "```\n",
    "sudo apt install -y libforms-dev libforms-bin\n",
    "```\n",
    "```\n",
    "./build_oai -w USRP --ninja --nrUE --gNB --build-lib \"nrscope\" -C\n",
    "```\n",
    "\n",
    "**Attention:** Be patient, the last command could take a whileeeee. If you want to follow the installation logs, run the command below:\n",
    "```\n",
    "tail -f /home/YOUR_USER/OAI/openairinterface5g/cmake_targets/log/all.txt\n",
    "```\n",
    "\n",
    "## Change Values in the Configuration Files\n",
    "\n",
    "It's necessary to change some configuration values in diferent files, so, let's start with the gNB.\n",
    "\n",
    "### OAIgNB Configuration for the Monolithic Mode\n",
    "\n",
    "**Step 1:** Configure the PLMN and TAC values\n",
    "\n",
    "The Public Land Mobile Network (PLMN) and Tracking Area Code (TAC) will be configured in the monolithic RAN (/home/YOUR_USER/OAI/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf) configuration file.\n",
    "\n",
    "Figure below shows the part of the code that must be modified in the monolithic file (lines 13 and 14). The parameters shown in figure should be modified according to the values present in docker compose respective file, in the \"oai-amf\" session (/home/YOUR_USER/OAI/oai-cn5g-fed/docker-compose/conf/basic_nrf_config.yaml).\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_06.png)\n",
    "\n",
    "Figure below shows the parameters of the plmn session that should be checked and passed to the monolithic configuration file: tac (referring to \"tracking_area_code\"), mcc (referring to \" plmn_list mcc\") and PLMN_SUPPORT_MNC (referring to \" plmn_listmnc\").\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_07.png)\n",
    "\n",
    "Figure below shows the result in the monolithic RAN file (/home/YOUR_USER/OAI/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf).\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_08.png)\n",
    "\n",
    "### nrE Configuration\n",
    "\n",
    "**Step 1:** Configure the IMSI, Permanent Key and OPC\n",
    "\n",
    "The UE parameters file is /home/YOUR_USER/OAI/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf. The first three lines of the file are composed by the IMSI, Permanent Key and OPC, as shown in figure below. You will configure these parameters.\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_09.png)\n",
    "\n",
    "You need to check in the docker-compose file (/home/YOUR_USER/OAI/oai-cn5g-fed/docker-compose/docker-compose-basic-nrf.yaml) which SQL file the core is using. See the figure below.\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_10.png)\n",
    "\n",
    "Verify/change the parameters in UE file (/home/YOUR_USER/OAI/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf) according to the SQL file illustrated in figure below. The parameters are:\n",
    "\n",
    "- \"ueid\" refers to \"imsi\"\n",
    "- \"encPermanentKey\" refers to \"key\"\n",
    "- \"encOpcKey\" refers to \"opc\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_11.png)\n",
    "\n",
    "Figure below shows the result in the UE configuration file (/home/YOUR_USER/OAI/openairinterface5g/targets/PROJECTS/GENERIC-NR-5GC/CONF/ue.conf).\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_12.png)\n",
    "\n",
    "## System Simulation\n",
    "\n",
    "Open 3 others terminals in the your home repository. The arrangement for the following steps can follow that shown in the figure below:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_13.png)\n",
    "\n",
    "In the first terminal, execute the commands below:\n",
    "```\n",
    "cd ~/OAI/oai-cn5g-fed/docker-compose\n",
    "```\n",
    "```\n",
    "docker compose -f docker-compose-basic-nrf.yaml up -d\n",
    "```\n",
    "In the second one follow the commands below:\n",
    "```\n",
    "cd ~/OAI/openairinterface5g/cmake_targets/ran_build/build\n",
    "```\n",
    "```\n",
    "sudo ./nr-softmodem -O ../../../targets/PROJECTS/GENERIC-NR-5GC/CONF/gnb.sa.band78.fr1.106PRB.usrpb210.conf --gNBs.[0].min_rxtxtime 6 --rfsim --sa\n",
    "```\n",
    "In the third, follow the commands below:\n",
    "```\n",
    "cd ~/OAI/openairinterface5g/cmake_targets/ran_build/build\n",
    "```\n",
    "```\n",
    "sudo ./nr-uesoftmodem -r 106 --numerology 1 --band 78 -C 3619200000 --sa --uicc0.imsi 001010000000001 --rfsim\n",
    "```\n",
    "\n",
    "**Step 2:** See logs of the 5GC's AMF container\n",
    "\n",
    "In the CN5G terminal, type:\n",
    "```\n",
    "docker logs -f oai-amf\n",
    "```\n",
    "The expected visualization is similar to the figure below:\n",
    "\n",
    "![5GC_deploy_02](../FIGS/H01_5G_Complete_Deployment/img_14.png)\n",
    "\n",
    "This mean that the gNB and the UE working in the other 2 terminals are connected to the CN5G.\n",
    "\n",
    "To leave this visualization press Ctrl + c.\n",
    "\n",
    "**Step 3:** Stoping the system simulation\n",
    "\n",
    "To stop the simulation, use the command below in the CN5G terminal:\n",
    "```\n",
    "docker compose -f docker-compose-basic-nrf.yaml down\n",
    "```\n",
    "And close the other 2 terminals.\n",
    "\n",
    "## Sources\n",
    "[CN5G](https://gitlab.eurecom.fr/oai/openairinterface5g/-/blob/develop/doc/NR_SA_Tutorial_OAI_CN5G.md)\n",
    "\n",
    "[OAI gNB and nrUE](https://gitlab.eurecom.fr/oai/openairinterface5g/-/blob/develop/doc/NR_SA_Tutorial_OAI_nrUE.md)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
